//
//  DMDomainManager.m
//  DualMediationSDK
//
//  Created by DualMediation on 2024/01/17.
//  Copyright © 2024 DualMediation. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "DMDomainManager.h"
#import "IMLogUtils.h"

// 广告策略、产品策略、登录接口域名常量
static NSString * const kAdStrategyProductLoginProduction = @"https://sapi.yarkgame.com";
static NSString * const kAdStrategyProductLoginDebug = @"https://sapi.yarkgame.com";

// 内购支付域名常量（继续使用现有域名）
static NSString * const kInAppPurchaseProduction = @"https://sapi.yarkgame.com";
static NSString * const kInAppPurchaseDebug = @"https://sapi.yarkgame.com";

@interface DMDomainManager ()

@property (nonatomic, assign) BOOL isDebugMode;

@end

@implementation DMDomainManager

#pragma mark - Singleton

+ (instancetype)sharedManager {
    static DMDomainManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupDefaultConfiguration];
    }
    return self;
}

#pragma mark - Configuration

- (void)setupDefaultConfiguration {
#ifdef DEBUG
    _isDebugMode = YES;
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 初始化为测试环境"];
#else
    _isDebugMode = NO;
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 初始化为生产环境"];
#endif
    
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 初始化域名管理器，当前环境: %@", [self currentEnvironmentDescription]];
    
    [self logCurrentDomainConfiguration];
}

#pragma mark - Public Methods

- (NSString *)domainForType:(DMDomainType)type {
    NSString *domain = nil;
    
    switch (type) {
        case DMDomainTypeAdStrategy:
        case DMDomainTypeProductStrategy:
        case DMDomainTypeLogin:
            domain = _isDebugMode ? kAdStrategyProductLoginDebug : kAdStrategyProductLoginProduction;
            break;
            
        case DMDomainTypeInAppPurchase:
            domain = _isDebugMode ? kInAppPurchaseDebug : kInAppPurchaseProduction;
            break;
            
        default:
            [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 未知的域名类型: %ld", (long)type];
            domain = _isDebugMode ? kAdStrategyProductLoginDebug : kAdStrategyProductLoginProduction;
            break;
    }
    
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 获取域名 - 类型: %@, 域名: %@", [self stringForDomainType:type], domain];
    return domain;
}

- (void)switchToProduction {
    if (!_isDebugMode) {
        [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 当前已是生产环境，无需切换"];
        return;
    }
    
    _isDebugMode = NO;
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 已切换到正式环境"];
    [self logCurrentDomainConfiguration];
}

- (void)switchToDebug {
    if (_isDebugMode) {
        [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 当前已是测试环境，无需切换"];
        return;
    }
    
    _isDebugMode = YES;
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 已切换到测试环境"];
    [self logCurrentDomainConfiguration];
}

- (NSString *)currentEnvironmentDescription {
    return _isDebugMode ? @"测试环境" : @"生产环境";
}

#pragma mark - Private Methods

- (void)logCurrentDomainConfiguration {
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] ========== 当前域名配置 (%@) ==========\n", [self currentEnvironmentDescription]];
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 广告策略域名: %@", [self domainForType:DMDomainTypeAdStrategy]];
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 产品策略域名: %@", [self domainForType:DMDomainTypeProductStrategy]];
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 登录接口域名: %@", [self domainForType:DMDomainTypeLogin]];
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] 内购支付域名: %@", [self domainForType:DMDomainTypeInAppPurchase]];
    [IMLogUtils info:IMLogTagGeneral format:@"[DMDomainManager] =======================================\n"];
}

- (NSString *)stringForDomainType:(DMDomainType)type {
    switch (type) {
        case DMDomainTypeAdStrategy:
            return @"广告策略";
        case DMDomainTypeProductStrategy:
            return @"产品策略";
        case DMDomainTypeLogin:
            return @"登录接口";
        case DMDomainTypeInAppPurchase:
            return @"内购支付";
        default:
            return @"未知类型";
    }
}

@end
